<template>
    <div class="bg">
        <div class="login-container">
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-position="left" label-width="0px"
                class="login-form">
                <h3 class="title">餐厅管理系统</h3>
                <el-form-item prop="username">
                    <el-input type="text" v-model="loginForm.username" autocomplete="off"
                        placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" autocomplete="off"
                        placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="repassword" autocomplete="off" placeholder="请输入确认密码"></el-input>
                </el-form-item>
                <el-form-item prop="role">
        <el-select v-model="loginForm.role"  style="width: 100%;" placeholder="请选择角色">
          <el-option label="管理员" value="ADMIN"></el-option>
          <el-option label="商家" value="BUSINESS"></el-option>
        </el-select>
      </el-form-item>
                <el-form-item style="width: 100%;">
                    <el-button type="primary" style="width: 100%;" @click="handleLogin">注册</el-button>
                </el-form-item>
                <div class="tip" @click="sjzhuce()">
                    已有账号？去登录
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import service from '@/request';

export default {
    data() {
        return {
            loginForm: {
                username: '',
                password: ''
            },
            repassword: "",
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
            }
        };
    },
    methods: {
        sjzhuce() {
            this.$router.push({
                path: "/"
            })
        },
        handleLogin() {
            this.$refs.loginForm.validate(async (valid) => {
                if (valid) {
                    if (this.repassword == this.loginForm.password) {
                        if(this.loginForm.role == 'BUSINESS'){
                            const res = await service.post("/admin/register", this.loginForm);
                        console.log(res)
                        if (res.code == 200) {
                            this.$message.success("注册成功");
                            this.$router.push({
                                path: "/"
                            })
                        

                        }
                        }else{
                            const res = await service.post("/admin/registeradmin", this.loginForm);
                        console.log(res)
                        if (res.code == 200) {
                            this.$message.success("注册成功");
                            this.$router.push({
                                path: "/"
                            })
                        

                        }
                        }
                        

                    } else {
                        this.$message.warning("密码和确认密码不一样")
                    }

                } else {
                    console.log('表单校验失败');
                    return false;
                }
            });
        }
    }
}
</script>

<style scoped>
.tip {
    color: #2a60c9;
    font-size: 14px;
    cursor: pointer;
    text-align: right;
}

.login-container {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border-radius: 4px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.title {
    text-align: center;
    margin-bottom: 20px;
    font-size: 24px;
    color: #333;
}

.login-form {
    width: 100%;
}

.el-input {
    width: 100%;
}

.bg {
    width: 100%;
    height: 100%;
    background: url("../assets/bg.png") no-repeat;
    background-size: 100% 100%;

}
</style>